//#include "stdafx.h"
#include <Windows.h>
#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include "SIX_Dict.h"

SIX_Dict::SIX_Dict()
{
	::srand((unsigned int)time(0));
}

SIX_Dict::~SIX_Dict()
{
}

int SIX_Dict::RNDNUM(int min,int max)
{
	int rndnum = 0;

	rndnum = rand() % (max-min+1) + min;

	if (rndnum<min)
		rndnum = min;
	return rndnum;
}

bool SIX_Dict::Encrypt(int Dit,const BYTE *Src,int SLen,BYTE *Dst)
{
	if (!Src || !SLen || Dit<0 || Dit>0xFF)
		return false;

	char tmp[5] = {0};
	int key = 0;

	for (int i=0;i<SLen;i++)
	{
		sprintf_s(tmp,4,"%02X",(unsigned char)Src[i]);
		key = strtoul(tmp,0,16);
		Dst[i] = g_DictA[Dit][key];
	}
	Dst[SLen] = 0;
	return true;
}

bool SIX_Dict::Decrypt(int Dit,const BYTE *Src,int SLen,BYTE *Dst)
{
	if (!Src || !SLen || Dit<0 || Dit>0xFF)
		return false;

	char tmp[5] = {0};
	int key = 0;

	for (int i=0;i<SLen;i++)
	{
		sprintf_s(tmp,4,"%02X",(unsigned char)Src[i]);
		key = strtoul(tmp,0,16);
		Dst[i] = g_DictB[Dit][key];
	}
	Dst[SLen] = 0;
	return true;
}